{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "反转:  [5, 4, 3, 2, 1]\n",
      "排序:  [5, 4, 3, 2, 1]\n",
      "计数1:  1\n",
      "继承后: [5, 4, 3, 2, 1, 100, 101]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 反转， 排序，计数, 继承, 清空\n",
    "# reverse, sort, count, extend, clear\n",
    "\n",
    "a = [1,2,3,4,5]\n",
    "print(a)\n",
    "a.reverse()\n",
    "print('反转: ', a)\n",
    "a.sort(reverse=True)\n",
    "print('排序: ', a)\n",
    "print('计数1: ', a.count(1))\n",
    "a.extend([100,101])\n",
    "print('继承后:', a)\n",
    "a.clear()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 1]\n"
     ]
    }
   ],
   "source": [
    "# 增\n",
    "a = []\n",
    "a.append(1)\n",
    "a.insert(0, 2)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5, 1]\n",
      "[1, 2, 3, 4, 1]\n",
      "[2, 3, 4, 1]\n"
     ]
    }
   ],
   "source": [
    "# 删    pop, remove, del函数\n",
    "a = [1,2,3,4,5,1]\n",
    "del a[0]\n",
    "print(a)\n",
    "a = [1,2,3,4,5,1]\n",
    "a.pop(-2)        \n",
    "print(a)\n",
    "a.remove(1)      # 没有1回报错, 只删除第一个匹配值\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 改  切片赋值\n",
    "a = [1,2,3,4,5]\n",
    "a[0] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "5\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "1 is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m/Users/apple/dsc/dsc/26.list常用方法/list_.ipynb Cell 5\u001b[0m in \u001b[0;36m<cell line: 6>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/26.list%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/list_.ipynb#W5sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39mprint\u001b[39m( a\u001b[39m.\u001b[39mindex(\u001b[39m1\u001b[39m) )\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/26.list%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/list_.ipynb#W5sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39mprint\u001b[39m( a\u001b[39m.\u001b[39mindex(\u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m))     \u001b[39m# 查找第二个\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/26.list%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/list_.ipynb#W5sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39mprint\u001b[39m( a\u001b[39m.\u001b[39;49mindex(\u001b[39m1\u001b[39;49m, \u001b[39m6\u001b[39;49m, \u001b[39m8\u001b[39;49m))  \u001b[39m# 在【6，8）查找，没有就报错\u001b[39;00m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/26.list%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/list_.ipynb#W5sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m \u001b[39mprint\u001b[39m( a\u001b[39m.\u001b[39mindex(\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m) )\n",
      "\u001b[0;31mValueError\u001b[0m: 1 is not in list"
     ]
    }
   ],
   "source": [
    "# 查询  index\n",
    "\n",
    "a = [1,2,3,4,5,1,2,3,1]\n",
    "print( a.index(1) )\n",
    "print( a.index(1, 2))     # 查找第二个\n",
    "print( a.index(1, 6, 8))  # 在【6，8）查找，没有就报错\n",
    "\n",
    "print( a.index(-1) )  # 没有则报错\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
